/**
 * Created by kroyo on 2017/8/17.
 * 需求详情页
 */





// nav-view切换
var $navlist = $('#container-other .demand-nav .dnav');
$navlist.click(function(){
    var index = $(this).index();
    var $nav_view = $('#container-other .nav-view>div');

    $navlist.removeClass('active');
    $(this).addClass('active');

    //切换视图
     animate(index);
     function animate(i){
         $nav_view.css('display','none');
         $nav_view.eq(i).css('display','block');
     }
})


// 1.1编辑需求  与  浏览需求之间的切换
var $watchbtn = $('#container-other .watch-demand');
var $compilebtn = $('#container-other .compile-demand');
var $demand_text = $('#container-other .demand-info');
$('#container-other .btn-compiledemand').click(function () {
    $watchbtn.css('display','none');
    $compilebtn.css('display','block');
    $demand_text.addClass('compile');
})
$('#container-other .btn-cancom').click(function () {
    $watchbtn.css('display','block');
    $compilebtn.css('display','none');
    $demand_text.removeClass('compile');
})


// 1.2取消需求
clickPalert('#container-other .btn-candemand','.win-calrequire','.btn-re');



//1.3编辑需求中  操作
$('#container-other form').change(function(){
    budget();
    deBudget();
    //服务内容
    changeShow('.service-wrap','s-content','.price-cate1','.price-cate2');
    //初始化设计预算显示模块  合作模式
    changeShow('.co-model','com-model','.price-wrap','.invest');
})
//设计预算隐藏
function budget(){
    var $value = $('#container-other .designschme .check-wrap input[type=checkbox]:checked');
    var $dbudget = $('#container-other .design-budget');
    if($value.length>0){
        $dbudget.css('display','block');
        $dbudget.css('opacity','1');
    }else{
        $dbudget.css('display','none');
        $dbudget.css('opacity','0');
    }
}
function changeShow(sel,name,sel1,sel2){
    var val = $(sel+' input:radio[name="'+name+'"]:checked').attr('data-val');
    var $selector1 = $('#container-other '+sel1);
    var $selector2 = $('#container-other '+sel2);
    if(val==0){
        $selector1.css('display','block');
        $selector2.css('display','none');
    }else{
        $selector1.css('display','none');
        $selector2.css('display','block');
    }
}
function deBudget(){
    var val = $('#container-other .budget .check-wrap input[type=checkbox]:checked');
    if(val.length>0){
        $('#container-other .de-budget').css('display','block');
    }else{
        $('#container-other .de-budget').css('display','none');
    }
}

//添加联系人弹窗
clickPowin('#container-other .btn-addtel','.win-addcontact',' .btn-close');


//编辑品类
clickPowin('.user-center .compile-catebtn','.win-category',' .btn-re');
$('.win-category .btn-close').click(function(){
    $('.win-category .btn-re').trigger('click');
})
//2文件管理
//2.1文件管理 之 上传文件
clickPowin('.view-filemana .btn-file','.win-uploadfile',' .btn-close');


// 3. 财务记录
// 3.1付款
clickPowin('.fin-record .btn-pay','.paryment-win',' .btn-reset');
//3.2 线下支付
$('.paryment-win .btn-conf').click(function(){
    var val = $('.paryment-win input:checked').data('val');
    // 关闭选项卡页 
    $('#web .paryment-win').fadeOut();
    resetData('.paryment-win');
    overfhidden();
    
    if(val==1){
        //跳转到线下支付凭证  页面
        var selwin = '.offpary-win';
        var $win = $('#web '+selwin);
        //弹出
        $win.fadeIn();
        overfhidden();
        // 关闭
        $(selwin+' .btn-reset').click(function(){
            $win.fadeOut();
            resetData(selwin);
            overfauto();
        })
    }
})


// 4.对接匹配
$('.dock-match button').on('click',function(){
    var id = $(this).index();
    switch (true) {
        case id==0:
            // 4.1发起合同
            scrollCon(3);
            contract(false);
            break;
        case id==1:
            // 4.2选定外观服务方
            scrollCon(id);
            break;
        case id==2:
            // 4.3选定结构服务方
            scrollCon(id);
            break;        
        default:
            break;
    }
})
//滚动方法
function scrollCon(i){
    var heighttop = $('.user-center .demand-pro').offset().top - $('#web .nav').height();
    $navlist.eq(i).trigger('click');
    
    
    $('html,body').animate({
       scrollTop: heighttop,
    },500);
}
//4.4选定服务方 提示   选定之后不能更改
clickPalert('.view-appematch .check-wrap','选定服务方后不可更改，请再次确认。');
clickPalert('.view-structmatch .check-wrap','选定服务方后不可更改，请再次确认。');




/******合同内容部分*******/ 


//4.1 控制合同内容是否可以编辑
var view_contact = '.view-contactinfo ';
function contract(off){
    var $con_wrap = $(view_contact);
    var $content = $(view_contact+' .contract-content'); 

    //合同  中  input输入框等
    var contact_input = view_contact+' input';
    //select 
    var contact_select = view_contact+' select';
    //textarea
    var contact_textarea = view_contact+' textarea';
    //多选按钮
    var contact_check = view_contact+' .check-wrap';
    //单选按钮
    var contact_radio = view_contact+' .radio-wrap';
    
    var $contact_check = $(view_contact+' .check-wrap');
    var $contact_radio = $(view_contact+' .radio-wrap');
    //签署合同  按钮
    var $contact_btn  = $(view_contact+' .agreement');

    //甲乙方
    var $party = $(view_contact+' .party');
    if(off){
        //甲乙方
        $party.removeClass('active');

        disable(contact_input);
        disable(contact_select);
        disable(contact_textarea);

        //阻止单选 、 多选 鼠标事件
        holdMouse(contact_check+' .check-text');
        holdMouse(contact_check+' .check');
        $contact_check.addClass('active');
        $contact_check.unbind('click');
        //单选
        holdMouse(contact_radio+' .check-text');
        holdMouse(contact_radio+' input');
        $contact_radio.addClass('achover');

        $contact_btn.addClass('no-startview');

    }else{
        $party.addClass('active');
        $content.css('background','none');
        removeDis(contact_select);
        removeDis(contact_textarea);
        removeDis(contact_check+' input');
        removeDis(contact_radio+' input');
        removeDis(view_contact+' .editable input');

        //阻止单选 、 多选 鼠标事件
        addMouse(contact_check+' .check-text');
        addMouse(contact_check+' .check');
        $contact_check.removeClass('active');
        addMouse(contact_radio+' .check-text');
        addMouse(contact_radio+' input');
        $contact_radio.removeClass('achover');

        $contact_btn.removeClass('no-startview');

        //change   事件
        $(wap).trigger('change');
    }
} 



// 4.2 合同编辑
var wap = '.contract-content ';

//金额计算值
var $total_appear = $(wap+' .design-deguet .total-appear');
var $total_conts  = $(wap+' .design-deguet .total-conts');
var $appear_before = $(wap+' .design-deguet .appear-before');
var $appear_end = $(wap+' .design-deguet .appear-end');
var $conts_before = $(wap+' .design-deguet .conts-before');
var $conts_end = $(wap+' .design-deguet .conts-end');


$(wap).change(function(event) {
    //外观设计
    selChange(' #appear',' .design-appear',' .design-deguet .design-appear');
    //结构设计
    selChange(' #contact',' .design-conts',' .design-deguet .design-conts');

    //计算外观或者结构总金额
    calculate($total_appear,$appear_before,$appear_end);
    calculate($total_conts,$conts_before,$conts_end);

    //计算总金额
    calculate($(wap+' .amount .total'),$total_appear,$total_conts);
    calculate($(wap+' .amount .total-before'),$appear_before,$conts_before);
    calculate($(wap+' .amount .total-end'),$appear_end,$conts_end);

    //增值服务
    if($(wap+' .add-service').length>0){
        addservice();
    }

    //其他 
    otherCheck(wap+' .texture .check-wrap');
    otherCheck(wap+' .cotrol .check-wrap');

});
$(wap).trigger('change');
contract(true);

//4.2.1设计方案的更换
function selChange(sel,selview,selinput){
    
    var $val = $(wap+sel);
    var $sel_view = $(wap+' .deliverable '+selview);
    var sel_input = wap+selinput+' input';

    if($val.is(':checked')){
        $sel_view.css('display','block');
        removeDis(sel_input);
    }else{
        $sel_view.css('display','none');
        disable(sel_input);

        //清空设计费用
        $(sel_input).val('');
    }
}
//4.2.2 其他这个选项 的事件
function otherCheck(sel){
    var $sel = $(sel).last();
    var $sel_input = $sel.next();
    var $input = $sel.children().eq(0);
    if($input.is(':checked')){
        $sel_input.removeAttr('disabled');
        $sel_input.removeAttr('readonly');
    }else{
        $sel_input.attr('disabled','true');
        $sel_input.attr('readonly','true');
    }
}

//4.2.3 项目金额计算
function calculate($sel,value1,value2){
    var val1 = parseFloat(value1.val());
    var val2 = parseFloat(value2.val());

    val1>=0?'':val1=0;
    val2>=0?'':val2=0;

    $sel.val(val1+val2);
}
//改变 结构设计总费用时, 预付与尾款平分
$total_conts.change(function(){
    var val = parseFloat($(this).val()*0.5);
    $conts_before.val(val);
    $conts_end.val(val);
});
$total_appear.change(function(){
    var val = parseFloat($(this).val()*0.5);
    $appear_before.val(val);
    $appear_end.val(val);
})

//4.2.4 增值服务
function addservice(){
    // 外观手板
    changfe();
    // 功能手板、外观专利
    changef(' .second ');
    changef(' .third .h-top');
    changef(' .third .h-down ');

    selectChange(wap+' .add-service .third .h-top','59','39');
    selectChange(wap+' .add-service .third .h-down','139','99');
}
function changfe(){
    var $first = $(wap+' .add-service #adds1');
    var nextinput = wap+' .add-service .first .select .check-wrap';
    var $nextinput = $(nextinput+' input');
    var input = $(nextinput).next();

    if($first.is(':checked')){
        removeDis($nextinput);
        addMouse(nextinput+' .check-text');
        addMouse(nextinput+' .check');
        $(nextinput).removeClass('active');

        //input 输入框可以输入
        changeinput($nextinput.first(),input.first());
        changeinput($nextinput.last(),input.last());

    }else{
        disable($(nextinput+' input'));
        holdMouse(nextinput+' .check-text');
        holdMouse(nextinput+' .check');
        $(nextinput).addClass('active');
    }
}
function changef(sel){
    var $sel = $(wap+' .add-service '+sel+' .check-wrap input').first();
    var input = wap+' .add-service '+sel+' .select input[type=text]';
    var $input = $(input);
    var nextinput = wap+' .add-service '+sel+' .select .check-wrap';
    if($sel.is(':checked')){
        removeDis(input);

        removeDis($(nextinput+' input'));
        addMouse(nextinput+' .check-text');
        addMouse(nextinput+' .check');
        $(nextinput).removeClass('active');

        //补偿险
        compens(wap+' .add-service .third .h-top ');
        compens(wap+' .add-service .third .h-down ');

    }else{
        disable(input);
        $input.val('');

        disable($(nextinput+' input'));
        holdMouse(nextinput+' .check-text');
        holdMouse(nextinput+' .check');
        $(nextinput).addClass('active');
    }
}
function changeinput($nextinput,input){
    if($nextinput.is(':checked')){
        removeDis(input);
    }else{
        disable(input);
        $(input).val('');
    }
}
//select 与 check-text 的更换
function selectChange(sel,num1,num2){
    var $select = $(sel+' .select select');
    var $span   = $(sel+' .select .check-wrap .check-text');
    var select_id = $select.get(0).selectedIndex;

    var pan = "<span class='check'></span>";

    if(select_id<=0){
        $span.html(pan+"费用补偿险（"+num1+"/件）");
    }else{
        $span.html(pan+'费用补偿险（'+num2+'/件）');
    }
}
//费用补偿险问题
function compens(sel){
    var $centerinput = $(sel+'.select .center input');
    $centerinput.on('input propertychange',function(){
        var $span = $(sel+' .select .down .num');
        $span.text($centerinput.val());
    });
}



// 表单元素 只读
function disable(sel){
    var $sel = $(sel);
    $sel.attr('disabled','true');
    $sel.attr('readonly','true');
}
//移除 事件
function removeDis(sel){
    var $sel = $(sel);
    $sel.removeAttr('disabled');
    $sel.removeAttr('readonly')
}
// 阻止所有鼠标事件
function holdMouse(sel){
    var $sel = $(sel);
    $sel.css('cursor','not-allowed');
}
// 添加鼠标事件 
function addMouse(sel){
    var $sel = $(sel);
    $sel.css('cursor','pointer');
}





//项目制、合伙人制的显示视图  true:项目制  ， false:合伙人制
changeView(true);
function changeView(off){
    // 外观设计 选择按钮
    var appearBtn = wap+' .design .appearbtn';
    //合伙人制
    var $partner_view = $(wap+' .partner-view');
    // 项目制
    var $project_view = $(wap+' .project-view');
    // true: 项目制 , false：合伙人制
    if(off){
        $partner_view.css('display','none');
        $project_view.css('display','table-row');
    }else{
        $partner_view.css('display','table-row');
        $project_view.css('display','none');

        $(wap).change(function(){
            // 控制外观设计  选择按钮不可更改
            disable(appearBtn+' input');
            holdMouse(appearBtn+' .check-text');
            holdMouse(appearBtn+' .check');
            $(appearBtn).addClass('active');

            //input 变成可编辑
            removeDis(wap+' .partner-view table input');

            //勾选结构设计
            var $contact = $(wap+' #contact');
            var coninput = wap+' .partner-view .inputk input';
            if($contact.is(':checked')){
                removeDis(coninput);
                //项目制   计算结构设计费用   总额改变  预付款与尾款平分
                $(coninput).first().change(function(){
                    var val = parseFloat($(this).val()*0.5);
                    $(coninput).eq(1).val(val);
                    $(coninput).eq(2).val(val);
                });

                //尾款  或 预付款改变时，总额也随着改变
                calculate($(coninput).first(),$(coninput).eq(1),$(coninput).eq(2));

            }else{
                disable(coninput);
                $(coninput).val('');
            }
        })
    }
}






// 5财务记录
// 5.1 选定开始计算提成的时间
$('.fin-record .tran-date button').click(function(){
    var selwin = '.win-alertview';
    var $win = $('#web '+selwin);
    var $text = $('#web '+selwin+' .title');
    var $val = $('.fin-record .tran-date input');
    if($val.val()!=''){
        $text.text('选定提成开始时间后不可更改，请再次确认。');
        $win.fadeIn();
        overfhidden();
    }
    // 关闭
    $(selwin+' .btn-re').click(function(){
        $win.fadeOut();
        overfauto();
    })
})
// 5.2 一键买断
clickPowin('.fin-record .deduct-view button','.win-buyout',' .btn-re');
//5.2.1 一键买断弹窗 中的立即支付按钮
clickPowin('.win-buyout .btn-conf','.paryment-win',' .btn-reset');
$('.win-buyout .btn-conf').click(function(){
    $('.win-buyout .btn-re').trigger('click');
})







/***公共方法***/

//点击事件  弹出弹窗
function clickPowin(selclick,selwin,selclose){
    var $win = $('#web '+selwin);
    //弹出
    $(selclick).click(function(){
        $win.fadeIn();
        overfhidden();
    })
    // 关闭
    $(selwin+selclose).click(function(){
        $win.fadeOut();
        resetData(selwin);
        overfauto();
    })
}
//提示框 
function clickPalert(selclick,text){
    var selwin = '.win-alertview';
    var $win = $('#web '+selwin);
    var $text = $('#web '+selwin+' .title');
    //弹出
    $(selclick).click(function(){
        $text.text(text);
        $win.fadeIn();
        overfhidden();
    })
    // 关闭
    $(selwin+' .btn-re').click(function(){
        $win.fadeOut();
        overfauto();
    })
}
//重置数据
function resetData(sel){
    $('#web '+sel+' input[type=reset]').trigger('click');
}

//弹出弹框时，隐藏于显示滚动条
function overfhidden(){
    $('body').css('overflow','hidden');
}
function overfauto(){
    $('body').css('overflow','auto');
}





changBox('.user-center input[type=checkbox]');
changBox('.win-uploadfile input[type=checkbox]');
changBox('.win-category input[type=checkbox]');
function changBox(sel){
    var $input = $(sel);
    $input.change(function(){
        $(this).parent().toggleClass('checked');
    })
}

//阻止 ‘外观、结构设计’的checkbox  的change事件
$('.view-match input[type=checkbox]').unbind('change');




//已登录
$('.nav .navlogin').first().addClass('active');
$('.no-login').css('display','none');
$('.a-login').css('display','block');
